<template>
    <s-layout id="layout" :tabbarshow="false" navbar="normal" :title="'发票列表'" :autoBack="true" leftIconColor="#333"
        :bgColor="'rgba(0,0,0,0)'" :titleStyle="{ color: '#fff', fontSize: '32rpx' }">
        <view id="app"
            :style="{ backgroundImage: `url( ${baseUrl}/uploads/uniapp_image/staff_v1/fapiaoguanli_bg1@2.png)`, paddingTop: (safeAreaInsets?.top + 44) + 'px' }">
            <view class="areasearch">
                <!-- 搜索组件 -->
                <view class="search-box">
                    <s-search-keyword :placeholder="'搜索发票'" @searchKeyWord="searchKeyWord"></s-search-keyword>
                </view>

            </view>
            <!-- 列表综合 -->
            <scroll-view scroll-y class="comprehensive" @scrolltolower="PopupScrolltolower" show-scrollbar="true"
                :refresher-enabled="isOpenRefresh" :refresher-triggered="triggered" :refresher-threshold="100"
                refresher-background="white" @refresherpulling="onPulling" @refresherrefresh="onRefresh"
                @refresherrestore="onRestore" @refresherabort="onAbort" @scroll="onScroll">
                <view v-if="invoiceList.length !== 0">
                  
                        <view class="list_item" v-for="(item, index) in invoiceList" :key="index" @tap="skipDetails(item.id)">
                            <view class="list_item_left">
                                <view class="icon">
                                    <image :src="`${baseUrl}/uploads/uniapp_image/staff_v1/shenpi_icon8@2.png`"
                                        mode="scaleToFill" />
                                </view>
                                <view class="message_box">
                                    <view class="message_title">{{ item.company_name }}</view>
                                    <view class="message_location">{{ item.address }}</view>
                                </view>
                            </view>

                            <view class="examine">查看详情> </view>
                        </view>
                
                    <!-- 正在加载组件 -->
                    <view>
                        <s-loadmore-gather :current_page="current_page" :last_page="last_page"
                            ref="loadmoreRef"></s-loadmore-gather>
                    </view>
                </view>
                <!-- 缺醒图 -->
                <view v-else style="width: 100%;height: 500rpx;background-color: #ffffff;border-radius: 20rpx;">
                    <u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png"></u-empty>
                    <view class="placeholde"></view>
                </view>

                <view class="null_box"></view>
            </scroll-view>
        </view>

    </s-layout>

    <!-- 底部保存按钮 -->
    <view v-if="jurisdiction.includes(group_id)">
        <s-button @netWork="newInvoice" :text="'新增发票'" />
    </view>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { onLoad, onShow } from '@dcloudio/uni-app';
import { baseUrl } from "@/sheep/config";
import sheep from '@/sheep';
import dayjs from 'dayjs';
const { safeAreaInsets } = uni.getSystemInfoSync();
const group_id = ref()
onLoad(() => {
    group_id.value = userStore.userInfo.group_id
    // invoiceIndexData()

    page.value = 1
    invoiceList.value = []
    invoiceIndexData()
})

onShow(() => {

})

const userStore = sheep.$store('user');
const jurisdiction = ref([4, 6])

// 封装列表接口
const page = ref(1)
const invoiceList = ref([])
const key_word = ref('')
const current_page = ref();
const last_page = ref();
const invoiceIndexData = async () => {
    let params = {
        filter: JSON.stringify({
            company_name: key_word.value,
        }),
        op: JSON.stringify({
            company_name: 'LIKE'
        }),
        page: page.value
    }
    const res = await sheep.$api.invoice.invoiceIndexAPI(params)
    invoiceList.value = [...invoiceList.value, ...res?.data.rows]
    current_page.value = res.data.current_page;
    last_page.value = res.data.last_page;
}

// 加载更多
const loadmoreRef = ref()
// 分页
const PopupScrolltolower = () => {
    loadmoreRef.value.PopupScrolltolower() // 加载更多
    if (current_page.value < last_page.value) {
        page.value += 1;
    } else {
        return;
    }
    invoiceIndexData()
}

// 搜索发票
const searchKeyWord = (e) => {
    invoiceList.value = []
    page.value = 1
    key_word.value = e
    invoiceIndexData()
}

// 跳转详情
const skipDetails = (id) => {
    sheep.$router.go('/subpackage/staff/workbench/invoice/invoice_details', {
        ids: id
    })
}

// 新增发票
const newInvoice = () => {
    sheep.$router.go('/subpackage/staff/workbench/invoice/invoice_add')
}
// 页面下拉刷新
const freshing = ref(false)
const triggered = ref(false)
const isOpenRefresh = ref(true)  // 是否开启下拉
// 自定义下拉刷新控件被下拉
const onPulling = (e) => {
    if (e.detail.deltaY < 0) return  // 防止上滑页面也触发下拉
    triggered.value = true;
}
// 自定义下拉刷新被触发
const onRefresh = () => {
    if (freshing.value) return;
    freshing.value = true;
    setTimeout(() => {
        triggered.value = false;
        freshing.value = false;
        invoiceList.value = []
        page.value = 1
        invoiceIndexData()
    }, 500);
}
// 自定义下拉刷新被复位
const onRestore = () => {
    triggered.value = 'restore'; // 需要重置
    // console.log("onRestore");
}
// 自定义下拉刷新被中止
const onAbort = () => {
    // console.log("onAbort");
}
</script>

<style scoped lang="scss">
page {
    width: 100%;
    height: 100%;
}

#app {
    background-size: 100%;
    width: 100%;
    min-height: 100%;
    background-repeat: no-repeat;
    background-attachment: fixed;
}

:deep(.uni-date__x-input) {
    width: 70% !important;
    padding: 0 !important;
    color: $uni-color-primary;
}

:deep(.uni-date-x--border) {
    border: none;
    width: 100%;
    padding: 0;
    color: $uni-color-primary;
}

:deep(.uni-date-x) {
    background-color: rgba(0, 0, 0, 0);
    padding: 0;
    color: $uni-color-primary !important;
}

:deep(.uni-date-editor) {
    width: 100%;
    color: $uni-color-primary;
}

.uni-date {
    width: 100%;
    color: $uni-color-primary;
}

.area {
    width: calc(100% - 40rpx);
    margin: 0 20rpx;

}
.areasearch{
    .search-box{
        margin: 240rpx auto 0 auto;
        width: calc(100% - 40rpx);
        :deep(.search_button) {
            background: linear-gradient(90deg, rgba(247, 156, 0, 1) 0.07%, rgba(254, 89, 7, 1) 100%);
        }

    }
}
:deep(.button) {
            background: linear-gradient(90deg, rgba(247, 156, 0, 1) 0.07%, rgba(254, 89, 7, 1) 100%) !important;
        }

// 滚动容器
.comprehensive {
    border-radius: 16rpx;
    width: calc(100% - 40rpx);
    background-color: rgba(0, 0, 0, 0);
    margin: 0 20rpx;
    height: 65vh;
    margin-top: 20rpx;
}

// 列表项
.list_item {
    height: 176rpx;
    border-radius: 8px;
    background-color: #FFFFFF;
    box-shadow: 0px 1px 4px 0px rgba(0, 0, 0, 0.05);
    margin-bottom: 20rpx;
    display: flex;
    justify-content: space-between;

    .list_item_left {
        display: flex;

        // 左 图标
        .icon {
            width: 96rpx;
            height: 96rpx;
            margin: 30rpx 30rpx 50rpx 20rpx;

            image {
                width: 100%;
                height: 100%;
            }
        }

        // 右 信息
        .message_box {

            // 信息标题
            .message_title {
                font-family: PingFangSC;
                color: $uni-text-color;
                font-weight: 700;
                font-size: 30rpx;
                font-style: normal;
                text-decoration: none;
                text-align: left;
                margin-bottom: 10rpx;
                margin-top: 30rpx;
                width: 354rpx;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
            }

            // 信息地址
            .message_location {
                font-family: PingFangSC;
                color: #999999;
                font-weight: 400;
                font-size: 22rpx;
                font-style: normal;
                text-decoration: none;
                text-align: left;
                width: 354rpx;
                white-space: normal;
            }
        }
    }

    // 详情
    .examine {
        font-family: PingFangSC;
        color: $uni-color-primary;
        font-weight: 400;
        font-size: 22rpx;
        font-style: normal;
        text-decoration: none;
        text-align: right;
        color: #FF6120;
        margin: 62rpx 40rpx 82rpx 0;
    }
}

// 占位
.placeholde {
    width: 100%;
    height: 50rpx;
    text-align: center;
    line-height: 50rpx;
    font-weight: $uni-font-weight;
    background-color: #ffffff;
    margin-bottom: 10rpx;
    border-bottom-left-radius: 16rpx;
    border-bottom-right-radius: 16rpx;
}

// 空盒子
.null_box {
    height: 72rpx;
}
:deep(.u-icon__icon) {
  background: $uni-bg-color;
  width: 64rpx;
  height: 64rpx;
  justify-content: center;
  border-radius: 50%;

}
</style>